package emap;

import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

//To Andrew: Sorry, I didn't write this completely on my own.
//I copied a few lines from an online tutorial. Is that okay?
//The only part from the tutorial are the implements and key(something) functions.
//Even those are modified. The mouse is all me. MWAHAHAHA!

class InputKey { //Holds a boolean saying whether or not it is pressed. Needs to be tied to an InputManager
	private boolean isPressed;
	private String keyName;
	private InputManager keySource;
	public int keyCode;
	public InputKey(){keyName="";keySource=null;}
	public InputKey(InputManager whereFrom){keyName="Some key";keySource=whereFrom;}
	public InputKey(String newName,InputManager whereFrom,int newKeyCode){keyName=new String(newName);keySource=whereFrom;keyCode=newKeyCode;}
	public boolean is(){return isPressed;}
	public void press(){isPressed=true;}
	public void release(){isPressed=false;}
	public String getKeyName(){return new String(keyName);}
	public InputManager getSource(){return keySource;}
	public void setKeyName(String a){keyName=new String(a);}
}

class InputManager implements KeyListener, MouseListener, MouseMotionListener {
	public boolean key_down;
	public boolean key_up;
	public boolean key_right;
	public boolean key_left;
	public boolean key_enter;
	public boolean key_space;
	public boolean key_escape;
	public boolean key_w;
	public boolean key_a;
	public boolean key_s;
	public boolean key_d;
	public boolean key_i;
	public boolean key_j;
	public boolean key_k;
	public boolean key_l;
	public boolean mouse_clicked;
	public Coord mouse_pos;
	private ArrayList<InputKey> inkeys;
	public InputManager(){
		mouse_pos=new Coord();
		inkeys=new ArrayList<InputKey>();
		registerKey(KeyEvent.VK_SHIFT,"Shift");
		}
	public InputKey registerKey(int a,String desc){
		for(InputKey b : inkeys){
			if(b.keyCode==a) return b; //It was already existent.
		}
		InputKey tmp=new InputKey(desc,this,a);
		inkeys.add(tmp);
		return tmp; //It was successfully added.
	}
	public InputKey getKeyOf(int theKeyCode){ //returns the InputKey that holds this key code. May return null!
		for(InputKey b : inkeys){
			if(b.keyCode==theKeyCode) return b;
		}
		return null;
	}
	public void keyTyped(KeyEvent e){}
	public void keyReleased(KeyEvent e)
		{
			if(e.getKeyCode() == e.VK_DOWN)
				key_down = false;
			if(e.getKeyCode() == e.VK_UP)
				key_up = false;
			if(e.getKeyCode() == e.VK_RIGHT)
				key_right = false;
			if(e.getKeyCode() == e.VK_LEFT)
				key_left = false;
			if(e.getKeyCode()==e.VK_ENTER)
				key_enter=false;
			if(e.getKeyCode()==e.VK_SPACE)
				key_space=false;
			if(e.getKeyCode()==e.VK_ESCAPE)
				key_escape=false;
			if(e.getKeyCode()==e.VK_W)
				key_w=false;
			if(e.getKeyCode()==e.VK_A)
				key_a=false;
			if(e.getKeyCode()==e.VK_S)
				key_s=false;
			if(e.getKeyCode()==e.VK_D)
				key_d=false;
			if(e.getKeyCode()==e.VK_I)
				key_i=false;
			if(e.getKeyCode()==e.VK_J)
				key_j=false;
			if(e.getKeyCode()==e.VK_K)
				key_k=false;
			if(e.getKeyCode()==e.VK_L)
				key_l=false;
			int tmpCode=e.getKeyCode();
			for(InputKey b : inkeys){
				if(b.keyCode==tmpCode){
					 b.release();
					 //System.out.println("The key "+b.getKeyName()+" was released.");
				}
			}
		}
	public void keyPressed(KeyEvent e){
		if(e.getKeyCode() == e.VK_DOWN)
			key_down = true;
		if(e.getKeyCode() == e.VK_UP)
			key_up = true;
		if(e.getKeyCode() == e.VK_RIGHT)
			key_right = true;
		if(e.getKeyCode() == e.VK_LEFT)
			key_left = true;
		if(e.getKeyCode()==e.VK_ENTER)
			key_enter=true;
		if(e.getKeyCode()==e.VK_SPACE)
			key_space=true;
		if(e.getKeyCode()==e.VK_ESCAPE)
			key_escape=true;
		if(e.getKeyCode()==e.VK_W)
			key_w=true;
		if(e.getKeyCode()==e.VK_A)
			key_a=true;
		if(e.getKeyCode()==e.VK_S)
			key_s=true;
		if(e.getKeyCode()==e.VK_D)
			key_d=true;
		if(e.getKeyCode()==e.VK_I)
			key_i=true;
		if(e.getKeyCode()==e.VK_J)
			key_j=true;
		if(e.getKeyCode()==e.VK_K)
			key_k=true;
		if(e.getKeyCode()==e.VK_L)
			key_l=true;
			int tmpCode=e.getKeyCode();
			for(InputKey b : inkeys){
				if(b.keyCode==tmpCode){
					 b.press();
					 //System.out.println("The key "+b.getKeyName()+" was pressed.");
				}
			}
	}
	public void updatemousepos(MouseEvent e){
		mouse_pos.X=e.getX();
		mouse_pos.Y=e.getY();
	}
	public void mouseClicked(MouseEvent e){updatemousepos(e);}
	public void mousePressed(MouseEvent e){
		updatemousepos(e);
		mouse_clicked=true;
	}
	public void mouseReleased(MouseEvent e){
		updatemousepos(e);
		mouse_clicked=false;
	}
	public void mouseEntered(MouseEvent e){updatemousepos(e);}
	public void mouseExited(MouseEvent e){
		updatemousepos(e);
		mouse_clicked=false;
	}
	public void mouseDragged(MouseEvent e){updatemousepos(e);}
	public void mouseMoved(MouseEvent e){updatemousepos(e);}
}